//
//  MouoTabBar.m
//  mouo
//
//  Created by tqh on 2017/1/9.
//  Copyright © 2017年 tqh. All rights reserved.
//

#import "IMETabBar.h"
#import "UIView+WJExtension.h"

@interface IMETabBar ()

@property (nonatomic, weak) UIButton *centerButton;/**中心发布按钮*/
@property (nonatomic,strong) UIImageView *bgImageView;
@property (nonatomic,strong) UIImageView *bgBottomView;

@end

@implementation IMETabBar

@dynamic delegate;

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.bgBottomView];
        [self addSubview:self.bgImageView];
        
        //mouo_tabbar_bg
        UIButton *centerButton = [[UIButton alloc] init];
        //mouo_tabbar_add
        [centerButton setImage:[UIImage imageNamed:@"mouo_tabbar_icon_select"] forState:UIControlStateHighlighted];
        [centerButton setImage:[UIImage imageNamed:@"mouo_tabbar_icon_normal"] forState:UIControlStateNormal];
        centerButton.size = CGSizeMake(34, 34);

        [centerButton addTarget:self action:@selector(centerButtonAction) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:centerButton];
        self.centerButton = centerButton;
    }
    return self;
}

#pragma mark - public

- (void)addCustomView:(UIView *)view {
    [self insertSubview:view atIndex:0];
}

#pragma mark - 事件监听

- (void)centerButtonAction
{
    
    if (self.imeDelegate && [self.delegate respondsToSelector:@selector(tabBarDidClickAtCenterButton:)]) {
        [self.imeDelegate tabBarDidClickAtCenterButton:self];
    }
}

#pragma mark - private

- (void)layoutSubviews
{

    [super layoutSubviews];
    self.centerButton.centerX = self.width * 0.5;
    if (ISIPHONEX) {
        self.centerButton.y = (self.height - 34) - 21 - 34;
    }else{
        self.centerButton.y = (self.height - 34) - 21;
    }
    
    CGFloat tabbarButtonW = self.width / 5;
    CGFloat tabbarButtonIndex = 0;
    for (UIView *child in self.subviews) {
        
        Class class = NSClassFromString(@"UITabBarButton");
        if ([child isKindOfClass:class]) {
            child.width = tabbarButtonW;
            child.x = tabbarButtonIndex * tabbarButtonW;
            child.height = 44;
            tabbarButtonIndex++;
            if (tabbarButtonIndex == 2) {
                tabbarButtonIndex++;
            }
        }
    }
    self.bgImageView.frame = CGRectMake(0, 0, SCREEN_WIDTH, self.height - TABBAREXTRAHEIGHT);
    self.bgBottomView.frame = CGRectMake(0, self.height - TABBAREXTRAHEIGHT, SCREEN_WIDTH, TABBAREXTRAHEIGHT);
}

#pragma mark - 懒加载

- (UIImageView *)bgImageView {
    if (!_bgImageView) {
        _bgImageView = [UIImageView new];
        _bgImageView.image = [UIImage imageNamed:@"mouo_tabbar_bg"];
    }
    return _bgImageView;
}

- (UIImageView *)bgBottomView {
    if (!_bgBottomView) {
        _bgBottomView = [UIImageView new];
        _bgBottomView.backgroundColor = [UIColor colorWithHexString:@"fafafa"];
    }
    return _bgBottomView;
}

#pragma mark - others

//这个方法不好用还要研究下，超出视图响应
-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    UIView * view = [super hitTest: point withEvent: event];
    if (view == nil && !self.hidden) {
        
        for (UIView * subView in self.subviews) {
            CGPoint myPoint = [subView convertPoint: point fromView: self];
            //隐藏了就不需要触发
            if (CGRectContainsPoint(subView.bounds, myPoint) && subView.hidden == NO && subView.userInteractionEnabled == YES) {
                if (subView.subviews.count > 0) {
                    //遍历子视图
                    for (UIView *obj in subView.subviews) {
                        CGPoint mysPoint = [obj convertPoint: myPoint fromView: subView];
                        if (CGRectContainsPoint(obj.bounds, mysPoint) ) {
                            return obj;
                        }
                    }
                }
                return subView;

            }
        }
    }
    return view;

}

@end
